﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AntdUI
{
    [Description("SignalLamp 信号灯")]
    [ToolboxItem(true)]
    public class SignalLamp: IControl
    {
        #region 属性
        private Timer blinkTimer;
        private bool isOn = true;  

        public SignalLamp(){
            this.DoubleBuffered = true;
            this.Size = new Size(100, 120);
            blinkTimer = new Timer();
            blinkTimer.Interval = BlinkInterval;
            blinkTimer.Tick += (s, e) => {
                isOn = !isOn;
                Invalidate();
            };
            blinkTimer.Start();

        }
         
        
        Color lampColor = ColorTranslator.FromHtml("#F8C44F");
    
        [Description("亮灯颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(Design.ColorEditor), typeof(UITypeEditor))]
        public Color LampColor
        {
            get => lampColor;
            set {
                if (lampColor == value) return;
                lampColor = value; 
                Invalidate();
                OnPropertyChanged(nameof(LampColor));
            }
        }

      
        Color lampOutColor = ColorTranslator.FromHtml("#CBCBCB");

        [Description("熄灯颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(Design.ColorEditor), typeof(UITypeEditor))]
        public Color LampOutColor
        {
            get => lampOutColor;
            set
            {
                if (lampOutColor == value) return;
                lampOutColor = value;
                Invalidate();
                OnPropertyChanged(nameof(LampOutColor));
            }
        }

        
        Color baseColor = ColorTranslator.FromHtml("#4169E1");
        [Description("灯座颜色"), Category("外观"), DefaultValue(null)]
        [Editor(typeof(Design.ColorEditor), typeof(UITypeEditor))]
        public Color BaseColor
        {
            get => baseColor;
            set {
                if (baseColor == value) return;
                baseColor = value; 
                Invalidate();
                OnPropertyChanged(nameof(BaseColor));
            }
        }

        int blinkInterval = 500;
        [Description("闪烁频率"), Category("数据"), DefaultValue(500)]
        public int BlinkInterval
        {
            get => blinkInterval;
            set {
                if (blinkInterval == value) return;
                blinkInterval = value; 
                blinkTimer.Interval = value;
                OnPropertyChanged(nameof(BlinkInterval));
            }
        }

        
        float baseHeightRatio = .1f;  
        [Description("底座高度占总控件高度的比例"), Category("外观"), DefaultValue(.1f)]
        public float BaseHeightRatio
        {
            get => baseHeightRatio;
            set
            {
                if (baseHeightRatio == value) return;
                baseHeightRatio = value; 
                Invalidate();
                OnPropertyChanged(nameof(BaseHeightRatio));
            }
        }

        float baseWidthRatio = 2f; 
        [Description("底座宽度是灯体宽度的比例"), Category("外观"), DefaultValue(2f)]
        public float BaseWidthRatio
        {
            get => baseWidthRatio;
            set { 
                if (baseHeightRatio == value) return; 
                baseWidthRatio = value; 
                Invalidate();
                OnPropertyChanged(nameof(BaseWidthRatio));
            }
        }

        float baseCornerRadius = 5f;  
        [Description("底座的圆角半径"), Category("外观"), DefaultValue(5f)]
        public float BaseCornerRadius
        {
            get => baseCornerRadius;
            set { 
                if (baseHeightRatio == value) return; 
                baseCornerRadius = value; 
                Invalidate();
                OnPropertyChanged(nameof(BaseCornerRadius));
            }
        }

        #endregion

        #region 方法

        [Description("闪烁"), Category("方法"), DefaultValue(null)]
        public void StartBlink()
        {
            blinkTimer.Start();
        }

        [Description("常熄"), Category("方法"), DefaultValue(null)]
        public void NormallyOff()
        {
            blinkTimer.Stop();
            isOn = false;   
            Invalidate();
            blinkTimer.Dispose();
        }
        [Description("常亮"), Category("方法"), DefaultValue(null)]
        public void NormallyOpen()
        {
            blinkTimer.Stop();
            isOn = true;
            Invalidate();
            blinkTimer.Dispose();
        }

        #endregion


        #region 动画
        protected override void Dispose(bool disposing)
        {
            blinkTimer?.Dispose();
            base.Dispose(disposing);
        }
        #endregion

        #region 渲染
        protected override void OnPaint(PaintEventArgs e)
        {
            var _rect = ClientRectangle;
            if (_rect.Width == 0 || _rect.Height==0) return;
            var rect = _rect.PaddingRect(Padding);
            var g = e.Graphics.High();

            float controlWidth = _rect.Width;
            float controlHeight = _rect.Height;
              
            float lampWidth = controlWidth * .6f;
            float lampHeight = controlHeight * .75f;
            float lampX = (controlWidth - lampWidth) / 2;
            float lampY = 0;

            float arcHeight = lampWidth / 2;
             
            float baseWidth = lampWidth * BaseWidthRatio;
            float baseHeight = controlHeight * BaseHeightRatio;
            float baseX = (controlWidth - baseWidth) / 2;
            float baseY = lampY + lampHeight;
             
            GraphicsPath lampBody = new GraphicsPath();
            lampBody.AddArc(lampX, lampY, lampWidth, arcHeight * 2, 180, 180);  
            lampBody.AddRectangle(new RectangleF(lampX, lampY + arcHeight, lampWidth, lampHeight - arcHeight));  
            using (Brush lampBrush = new SolidBrush(isOn ? LampColor : lampOutColor))
            {
                g.Fill(lampBrush, lampBody);
            }
             
            using (var path = new GraphicsPath())
            {
                float x = baseX, y = baseY, width = baseWidth, height = baseHeight, radius = BaseCornerRadius;
                path.AddArc(x, y, radius * 2, radius * 2, 180, 90);  
                path.AddArc(x + width - radius * 2, y, radius * 2, radius * 2, 270, 90);  
                path.AddArc(x + width - radius * 2, y + height - radius * 2, radius * 2, radius * 2, 0, 90);  
                path.AddArc(x, y + height - radius * 2, radius * 2, radius * 2, 90, 90);  
                path.CloseFigure(); 
                using (Brush baseBrush = new SolidBrush(BaseColor))
                    g.Fill(baseBrush, path);
            } 

        }
         
        #endregion

    }
}
